<?php 
extract($options);

$modelObj = cri($model); 
$commentModelAlias = $modelObj->actsAs['Comments.Commentable']['commentModel'];

$commentModel = cri($commentModelAlias);
$allowedModelsToPost = $commentModel->actsAs['Users.UserContent']['posterModels'];

$loggedInModel = $session->read('Auth.model');

$allowed = false;
$guest = false;

if(($loggedInModel) && isset($modelObj->{$loggedInModel}->className) &&  in_array($modelObj->{$loggedInModel}->className, $allowedModelsToPost))
{
	$allowed = true;
}
else
{
	if(in_array('Users.Guest', $allowedModelsToPost))
	{
		$allowed = $guest = true;
	}
}
?>
<?php if($allowed) { ?>

	<h3>Add a comment</h3>
	<?php
		if(!isset($url))
		{
			$url = '/comments/add/'.$settings['type'].'/'.$settings['item_id'];
		}
	echo $form->create($commentModelAlias, array('url'=>$url,'id'=>'add_comment'));
	if($guest)
	{
		$guestFields = cri('Users.Guest')->getFields('group', 'add');
		foreach($guestFields as $field)
		{
			echo $form->input('Guest.'.$field);
		}
	}
	$commentFields = $commentModel->getFields('group', 'add');
	
	$commentFields = am($commentFields, array('legend' => false, 'fieldset'=>false));
	echo $form->inputs($commentFields);
	echo $form->input($commentModel->alias.'.'.Inflector::underscore($modelObj->alias).'_id',array('type'=>'hidden','value'=>$settings['item_id']));
	echo $form->end(__('Submit'));
	?>
	
<?php } else { ?>
	
	You need to <a href="<?php echo $html->url('/');?>">login</a> before you can leave any comment on this page.

<?php } ?>
<script type="text/javascript" src="/cms/js/jquery.validate.min.js"></script>
<script type='text/javascript'>
$(document).ready(function()
{
	$('#add_comment input').addClass('required');
	$('#PostCommentBody').addClass('required');
	$('#GuestEmail').addClass('email');
	
	$('.submit').live('click',function(e)
	{		
		if($("#add_comment").valid()==false)
		{
			return;
		}
	});
});
</script>